/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.purchaseorder.service;

import java.math.BigDecimal;
import java.util.List;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.SnowFlake;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.mapper.AccountMapper;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.dynamicrecord.entity.DynamicRecord;
import com.jeeplus.modules.freezingassets.entity.FreezingAssets;
import com.jeeplus.modules.freezingassets.mapper.FreezingAssetsMapper;
import com.jeeplus.modules.freezingassets.service.FreezingAssetsService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.positionassets.entity.PositionAssets;
import com.jeeplus.modules.positionassets.mapper.PositionAssetsMapper;
import com.jeeplus.modules.positionassets.service.PositionAssetsService;
import com.jeeplus.modules.rewardparameters.entity.RewardParameters;
import com.jeeplus.modules.saleorder.entity.SaleOrder;
import com.jeeplus.modules.saleorder.mapper.SaleOrderMapper;
import com.jeeplus.modules.saleorder.service.SaleOrderService;
import com.jeeplus.modules.subgoods.entity.SubGoods;
import com.jeeplus.modules.subgoods.service.SubGoodsService;
import com.jeeplus.modules.teamservice.service.TeamServiceService;
import com.jeeplus.modules.traderecord.entity.TradeRecord;
import com.jeeplus.modules.traderecord.mapper.TradeRecordMapper;
import com.jeeplus.modules.traderecord.service.TradeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.purchaseorder.entity.PurchaseOrder;
import com.jeeplus.modules.purchaseorder.mapper.PurchaseOrderMapper;

/**
 * 买入订单Service
 * @author w
 * @version 2021-03-27
 */
@Service
@Transactional(readOnly = true)
public class PurchaseOrderService extends CrudService<PurchaseOrderMapper, PurchaseOrder> {
	@Autowired
	private SubGoodsService subGoodsService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private AccountMapper accountMapper;
	@Autowired
	private SaleOrderService saleOrderService;
	@Autowired
	private SaleOrderMapper saleOrderMapper;
	@Autowired
	private TradeRecordService tradeRecordService;
	@Autowired
	private TradeRecordMapper tradeRecordMapper;
	@Autowired
	private MemberService memberService;
	@Autowired
	private PositionAssetsService positionAssetsService;
	@Autowired
	private PositionAssetsMapper positionAssetsMapper;
	@Autowired
	private FreezingAssetsService freezingAssetsService;
	@Autowired
	private FreezingAssetsMapper freezingAssetsMapper;
	@Autowired
	private TeamServiceService teamServiceService;

	public PurchaseOrder get(String id) {
		return super.get(id);
	}
	
	public List<PurchaseOrder> findList(PurchaseOrder purchaseOrder) {
		return super.findList(purchaseOrder);
	}
	
	public Page<PurchaseOrder> findPage(Page<PurchaseOrder> page, PurchaseOrder purchaseOrder) {
		return super.findPage(page, purchaseOrder);
	}
	
	@Transactional(readOnly = false)
	public void save(PurchaseOrder purchaseOrder) {
		super.save(purchaseOrder);
	}
	
	@Transactional(readOnly = false)
	public void delete(PurchaseOrder purchaseOrder) {
		super.delete(purchaseOrder);
	}

	/**
	 * 买入
	 * @param goodsId
	 * @param member
	 * @param price
	 * @param num
	 * @param totalPrice
	 *@param charge  @return
	 */
	@Transactional(readOnly = false)
	public synchronized AjaxJson purchaseGoods(String goodsId, Member member, double price, int num, double charge, double totalPrice) {
		AjaxJson aj = new AjaxJson();
		SubGoods subGoods = subGoodsService.get(goodsId);
		if (subGoods != null){
			if (!"1".equals(subGoods.getIsTrade())){
				aj.setSuccess(false);
				aj.setMsg("该商品暂不可交易");
				return aj;
			}
//			TradeRecord t = new TradeRecord();
//			t.setSubGoods(subGoods);
			List<TradeRecord> tlist = tradeRecordService.findYesterdayBeforeInfo(subGoods.getId());
			if (tlist.size()>0){
				BigDecimal upMoney = BigDecimal.valueOf(price).subtract(BigDecimal.valueOf(tlist.get(0).getPrice()));
				BigDecimal up = upMoney.divide(BigDecimal.valueOf(tlist.get(0).getPrice()),2,BigDecimal.ROUND_HALF_DOWN);
				if (up.doubleValue() > 0.1 || up.doubleValue() < -0.1){
					aj.setSuccess(false);
					aj.setMsg("价格跌涨幅过大，请调整");
					return aj;
				}
			}else {
				BigDecimal upMoney = BigDecimal.valueOf(price).subtract(BigDecimal.valueOf(subGoods.getPrice()));
				BigDecimal up = upMoney.divide(BigDecimal.valueOf(subGoods.getPrice()),2,BigDecimal.ROUND_HALF_DOWN);
				if (up.doubleValue() > 0.1 || up.doubleValue() < -0.1){
					aj.setSuccess(false);
					aj.setMsg("价格跌涨幅过大，请调整");
					return aj;
				}
			}
			//查询已挂待交易的买入订单
			PurchaseOrder p = new PurchaseOrder();
			p.setMember(member);
			p.setIsRevoke("0");
			p.setIsDeal("0");
			List<PurchaseOrder> plist = findList(p);
			double totalMoney = 0.0;
			if (plist.size()>0){
				totalMoney = plist.stream().mapToDouble(PurchaseOrder::getTotalPrice).sum();
			}
			//todo 判读余额是否充足
			Account account = accountService.get(member.getAccount().getId());
			if (account != null){
				if (account.getYue()-totalMoney >= (totalPrice+charge)){

					SnowFlake snowFlake = new SnowFlake(2, 3);
					//生成订单编号
					Long orderFormNumber = snowFlake.nextId();
					String orderNo = orderFormNumber.toString();
					//剩余应锁仓数量
//					int freezingNum = BigDecimal.valueOf(num).multiply(BigDecimal.valueOf(0.5)).setScale(0,BigDecimal.ROUND_DOWN).intValue();
					//新建买入订单
					PurchaseOrder purchaseOrder = new PurchaseOrder();
					purchaseOrder.setId(IdGen.uuid());
					purchaseOrder.setOrderNo(orderNo);
					purchaseOrder.setCharge(charge);
					purchaseOrder.setIsDeal("0");
					purchaseOrder.setIsRevoke("0");
					purchaseOrder.setMember(member);
					purchaseOrder.setNonNum(num);
					purchaseOrder.setNum(num);
					purchaseOrder.setFreezingQuantity(0);
					purchaseOrder.setPrice(price);
					purchaseOrder.setSubGoods(subGoods);
					purchaseOrder.setTotalPrice(totalPrice);
					purchaseOrder.preInsert();
					mapper.insert(purchaseOrder);
					//扣除手续费先
//					account.setYue(account.getYue()-charge);
//					accountService.save(account);
					//匹配卖出订单   卖出单价在此单价以下的
					SaleOrder saleOrder = new SaleOrder();
					saleOrder.setSubGoods(subGoods);
					saleOrder.setPrice(price);
					List<SaleOrder> saleOrderList = saleOrderService.findStandardSaleOrders(saleOrder);
					if (saleOrderList.size()>0){
						for (SaleOrder order : saleOrderList) {
							if (!order.getMember().getId().equals(member.getId())){
								//计算每单手续费
								double c = BigDecimal.valueOf(order.getCharge()).divide(BigDecimal.valueOf(order.getNum()),2,BigDecimal.ROUND_HALF_DOWN).doubleValue();
								Member member1 = memberService.get(order.getMember().getId());
								//匹到的卖出订单中 卖出数量少
								if (order.getNonNum() < num){
									//成交数量
									Integer dealNum = order.getNonNum();
									order.setNonNum(0);
									order.setIsDeal("1");
									order.preUpdate();
									saleOrderMapper.update(order);
									//给卖出的用户加卖出去的钱
									Member saleMember = memberService.get(order.getMember().getId());
									Account saleAccount = accountService.get(saleMember.getAccount().getId());
									saleAccount.setYue(saleAccount.getYue()+(order.getPrice()*dealNum)-(c*dealNum));
									saleAccount.preUpdate();
									accountMapper.update(saleAccount);
									//给卖出用户减去持仓
									PositionAssets sp = new PositionAssets();
									sp.setMember(saleMember);
									sp.setSubGoods(subGoods);
									sp.setIsApply("0");
									List<PositionAssets> saleAssets = positionAssetsService.findList(sp);
									if (saleAssets.size()>0){
										saleAssets.get(0).setGoodsNum(saleAssets.get(0).getGoodsNum()-dealNum);
										saleAssets.get(0).preUpdate();
										positionAssetsMapper.update(saleAssets.get(0));
									}
									//买入订单扣除买入数量
									purchaseOrder.setNonNum(purchaseOrder.getNonNum()-dealNum);
									if (dealNum>=purchaseOrder.getFreezingQuantity()){
										//给买入的用户加持仓
										PositionAssets positionAssets = new PositionAssets();
										positionAssets.setMember(member);
										positionAssets.setSubGoods(subGoods);
										positionAssets.setIsApply("0");
										List<PositionAssets> pp = positionAssetsService.findList(positionAssets);
										if (pp.size()>0){
											pp.get(0).setGoodsNum(pp.get(0).getGoodsNum()+(dealNum-purchaseOrder.getFreezingQuantity()));
											pp.get(0).preUpdate();
											positionAssetsMapper.update(pp.get(0));
										}else {
											PositionAssets positionAssets1 = new PositionAssets();
											positionAssets1.setId(IdGen.uuid());
											positionAssets1.setMember(member);
											positionAssets1.setGoodsNum(dealNum-purchaseOrder.getFreezingQuantity());
											positionAssets1.setSubGoods(subGoods);
											positionAssets1.setGoodsName(subGoods.getGoodsName());
											positionAssets1.setGoodsPrice(order.getPrice());
											positionAssets1.setTotalPrice(order.getPrice()*(dealNum-purchaseOrder.getFreezingQuantity()));
											positionAssets1.setIsApply("0");
											positionAssets1.setApplyNum(0);
											positionAssets1.preInsert();
											positionAssetsMapper.insert(positionAssets1);
										}
										//给买入的用户加锁仓
//										FreezingAssets freezingAssets = new FreezingAssets();
//										freezingAssets.setMember(member);
//										freezingAssets.setSubGoods(subGoods);
//										List<FreezingAssets> pf = freezingAssetsService.findList(freezingAssets);
//										if (pf.size()>0){
//											pf.get(0).setNum(pf.get(0).getNum()+purchaseOrder.getFreezingQuantity());
//											freezingAssetsService.save(pf.get(0));
//										}else {
//											FreezingAssets freezingAssets1 = new FreezingAssets();
//											freezingAssets1.setMember(member);
//											freezingAssets1.setSubGoods(subGoods);
//											freezingAssets1.setGoodsName(subGoods.getGoodsName());
//											freezingAssets1.setNum(purchaseOrder.getFreezingQuantity());
//											freezingAssets1.setGoodsPrice(order.getPrice());
//											freezingAssets1.setTotalPrice(order.getPrice()*purchaseOrder.getFreezingQuantity());
//											freezingAssetsService.save(freezingAssets1);
//										}
//										purchaseOrder.setFreezingQuantity(0);
									}
//									else {
//										//只加锁仓
//										//给买入的用户加锁仓
//										FreezingAssets freezingAssets = new FreezingAssets();
//										freezingAssets.setMember(member);
//										freezingAssets.setSubGoods(subGoods);
//										List<FreezingAssets> pf = freezingAssetsService.findList(freezingAssets);
//										if (pf.size()>0){
//											pf.get(0).setNum(pf.get(0).getNum()+dealNum);
//											freezingAssetsService.save(pf.get(0));
//										}else {
//											FreezingAssets freezingAssets1 = new FreezingAssets();
//											freezingAssets1.setMember(member);
//											freezingAssets1.setSubGoods(subGoods);
//											freezingAssets1.setGoodsName(subGoods.getGoodsName());
//											freezingAssets1.setNum(purchaseOrder.getFreezingQuantity()-dealNum);
//											freezingAssets1.setGoodsPrice(order.getPrice());
//											freezingAssets1.setTotalPrice(order.getPrice()*(purchaseOrder.getFreezingQuantity()-dealNum));
//										}
//										purchaseOrder.setFreezingQuantity(purchaseOrder.getFreezingQuantity()-dealNum);
//									}
									mapper.update(purchaseOrder);
									//给买入账户扣钱
									account.setYue(account.getYue()-(order.getPrice()*dealNum));
									account.preUpdate();
									accountMapper.update(account);
									//成交记录
									TradeRecord tradeRecord = new TradeRecord();
									tradeRecord.setId(IdGen.uuid());
									tradeRecord.setSubGoods(subGoods);
									tradeRecord.setNum(dealNum);
									tradeRecord.setPrice(order.getPrice());
									tradeRecord.setPurchaseMember(member);
									tradeRecord.setPurchaseOrder(orderNo);
									tradeRecord.setSaleMember(order.getMember());
									tradeRecord.setSaleOrderNo(order.getOrderNo());
									tradeRecord.setTotalPrice(dealNum*order.getPrice());
									tradeRecord.preInsert();
									tradeRecordMapper.insert(tradeRecord);

									//给上级加业绩
//									addManagersTeam(member,dealNum*order.getPrice());
									//更新成交价
									manageTradePrice(subGoods,order);
									teamServiceService.manageTeamService(member,c*dealNum,orderNo);
									if ("18888888888".equals(member.getLoginName())){
										subGoodsService.addStock(subGoods,dealNum);
									}
									if ("18888888888".equals(member1.getLoginName())){
										subGoodsService.substractStock(subGoods,dealNum);
									}

									num -= dealNum;
								}else if (order.getNonNum() == num){
									//成交数量
									Integer dealNum = order.getNonNum();
									order.setNonNum(0);
									order.setIsDeal("1");
									order.preUpdate();
									saleOrderMapper.update(order);
									//给卖出的用户加卖出去的钱
									Member saleMember = memberService.get(order.getMember().getId());
									Account saleAccount = accountService.get(saleMember.getAccount().getId());
									saleAccount.setYue(saleAccount.getYue()+(order.getPrice()*dealNum)-(c*dealNum));
									saleAccount.preUpdate();
									accountMapper.update(saleAccount);
									//给卖出用户减去持仓
									PositionAssets sp = new PositionAssets();
									sp.setMember(saleMember);
									sp.setSubGoods(subGoods);
									sp.setIsApply("0");
									List<PositionAssets> saleAssets = positionAssetsService.findList(sp);
									if (saleAssets.size()>0){
										saleAssets.get(0).setGoodsNum(saleAssets.get(0).getGoodsNum()-dealNum);
										saleAssets.get(0).preUpdate();
										positionAssetsMapper.update(saleAssets.get(0));
									}
									//买入订单扣除买入数量
									purchaseOrder.setNonNum(0);
									if (dealNum>=purchaseOrder.getFreezingQuantity()){
										//给买入的用户加持仓
										PositionAssets positionAssets = new PositionAssets();
										positionAssets.setMember(member);
										positionAssets.setSubGoods(subGoods);
										positionAssets.setIsApply("0");
										List<PositionAssets> pp = positionAssetsService.findList(positionAssets);
										if (pp.size()>0){
											pp.get(0).setGoodsNum(pp.get(0).getGoodsNum()+(dealNum-purchaseOrder.getFreezingQuantity()));
											pp.get(0).preUpdate();
											positionAssetsMapper.update(pp.get(0));
										}else {
											PositionAssets positionAssets1 = new PositionAssets();
											positionAssets1.setId(IdGen.uuid());
											positionAssets1.setMember(member);
											positionAssets1.setGoodsNum(dealNum-purchaseOrder.getFreezingQuantity());
											positionAssets1.setSubGoods(subGoods);
											positionAssets1.setGoodsName(subGoods.getGoodsName());
											positionAssets1.setGoodsPrice(order.getPrice());
											positionAssets1.setTotalPrice(order.getPrice()*(dealNum-purchaseOrder.getFreezingQuantity()));
											positionAssets1.setIsApply("0");
											positionAssets1.setApplyNum(0);
											positionAssets1.preInsert();
											positionAssetsMapper.insert(positionAssets1);
										}
										//给买入的用户加锁仓
//										FreezingAssets freezingAssets = new FreezingAssets();
//										freezingAssets.setMember(member);
//										freezingAssets.setSubGoods(subGoods);
//										List<FreezingAssets> pf = freezingAssetsService.findList(freezingAssets);
//										if (pf.size()>0){
//											pf.get(0).setNum(pf.get(0).getNum()+purchaseOrder.getFreezingQuantity());
//											freezingAssetsService.save(pf.get(0));
//										}else {
//											FreezingAssets freezingAssets1 = new FreezingAssets();
//											freezingAssets1.setMember(member);
//											freezingAssets1.setSubGoods(subGoods);
//											freezingAssets1.setGoodsName(subGoods.getGoodsName());
//											freezingAssets1.setNum(purchaseOrder.getFreezingQuantity());
//											freezingAssets1.setGoodsPrice(order.getPrice());
//											freezingAssets1.setTotalPrice(order.getPrice()*purchaseOrder.getFreezingQuantity());
//											freezingAssetsService.save(freezingAssets1);
//										}
//										purchaseOrder.setFreezingQuantity(0);
									}
//									else {
//										//只加锁仓
//										//给买入的用户加锁仓
//										FreezingAssets freezingAssets = new FreezingAssets();
//										freezingAssets.setMember(member);
//										freezingAssets.setSubGoods(subGoods);
//										List<FreezingAssets> pf = freezingAssetsService.findList(freezingAssets);
//										if (pf.size()>0){
//											pf.get(0).setNum(pf.get(0).getNum()+dealNum);
//											freezingAssetsService.save(pf.get(0));
//										}else {
//											FreezingAssets freezingAssets1 = new FreezingAssets();
//											freezingAssets1.setMember(member);
//											freezingAssets1.setSubGoods(subGoods);
//											freezingAssets1.setGoodsName(subGoods.getGoodsName());
//											freezingAssets1.setNum(purchaseOrder.getFreezingQuantity()-dealNum);
//											freezingAssets1.setGoodsPrice(order.getPrice());
//											freezingAssets1.setTotalPrice(order.getPrice()*(purchaseOrder.getFreezingQuantity()-dealNum));
//										}
//										purchaseOrder.setFreezingQuantity(purchaseOrder.getFreezingQuantity()-dealNum);
//									}
									purchaseOrder.setIsDeal("1");
									purchaseOrder.preUpdate();
									mapper.update(purchaseOrder);
									//给买入账户扣钱
									account.setYue(account.getYue()-(order.getPrice()*dealNum));
									account.preUpdate();
									accountMapper.update(account);
									//成交记录
									TradeRecord tradeRecord = new TradeRecord();
									tradeRecord.setId(IdGen.uuid());
									tradeRecord.setSubGoods(subGoods);
									tradeRecord.setNum(dealNum);
									tradeRecord.setPrice(order.getPrice());
									tradeRecord.setPurchaseMember(member);
									tradeRecord.setPurchaseOrder(orderNo);
									tradeRecord.setSaleMember(order.getMember());
									tradeRecord.setSaleOrderNo(order.getOrderNo());
									tradeRecord.setTotalPrice(dealNum*order.getPrice());
									tradeRecord.preInsert();
									tradeRecordMapper.insert(tradeRecord);
									//给上级加业绩
//									addManagersTeam(member,dealNum*order.getPrice());
									//更新成交价
									manageTradePrice(subGoods,order);
									teamServiceService.manageTeamService(member,c*dealNum,orderNo);
									if ("18888888888".equals(member.getLoginName())){
										subGoodsService.addStock(subGoods,dealNum);
									}
									if ("18888888888".equals(member1.getLoginName())){
										subGoodsService.substractStock(subGoods,dealNum);
									}
									break;
								}else if (order.getNonNum() > num){
									//成交数量
									Integer dealNum = num;
									order.setNonNum(order.getNonNum()-num);
									order.preUpdate();
									saleOrderMapper.update(order);
									//给卖出的用户加卖出去的钱
									Member saleMember = memberService.get(order.getMember().getId());
									Account saleAccount = accountService.get(saleMember.getAccount().getId());
									saleAccount.setYue(saleAccount.getYue()+(order.getPrice()*num)-(c*dealNum));
									saleAccount.preUpdate();
									accountMapper.update(saleAccount);
									//给卖出用户减去持仓
									PositionAssets sp = new PositionAssets();
									sp.setMember(saleMember);
									sp.setSubGoods(subGoods);
									sp.setIsApply("0");
									List<PositionAssets> saleAssets = positionAssetsService.findList(sp);
									if (saleAssets.size()>0){
										saleAssets.get(0).setGoodsNum(saleAssets.get(0).getGoodsNum()-dealNum);
										saleAssets.get(0).preUpdate();
										positionAssetsMapper.update(saleAssets.get(0));
									}
									//买入订单扣除买入数量
									purchaseOrder.setNonNum(0);
									if (dealNum>=purchaseOrder.getFreezingQuantity()){
										//给买入的用户加持仓
										PositionAssets positionAssets = new PositionAssets();
										positionAssets.setMember(member);
										positionAssets.setSubGoods(subGoods);
										positionAssets.setIsApply("0");
										List<PositionAssets> pp = positionAssetsService.findList(positionAssets);
										if (pp.size()>0){
											pp.get(0).setGoodsNum(pp.get(0).getGoodsNum()+(dealNum-purchaseOrder.getFreezingQuantity()));
											pp.get(0).preUpdate();
											positionAssetsMapper.update(pp.get(0));
										}else {
											PositionAssets positionAssets1 = new PositionAssets();
											positionAssets1.setId(IdGen.uuid());
											positionAssets1.setMember(member);
											positionAssets1.setGoodsNum(dealNum-purchaseOrder.getFreezingQuantity());
											positionAssets1.setSubGoods(subGoods);
											positionAssets1.setGoodsName(subGoods.getGoodsName());
											positionAssets1.setGoodsPrice(order.getPrice());
											positionAssets1.setTotalPrice(order.getPrice()*(dealNum-purchaseOrder.getFreezingQuantity()));
											positionAssets1.setIsApply("0");
											positionAssets1.setApplyNum(0);
											positionAssets1.preInsert();
											positionAssetsMapper.insert(positionAssets1);
										}
										//给买入的用户加锁仓
//										FreezingAssets freezingAssets = new FreezingAssets();
//										freezingAssets.setMember(member);
//										freezingAssets.setSubGoods(subGoods);
//										List<FreezingAssets> pf = freezingAssetsService.findList(freezingAssets);
//										if (pf.size()>0){
//											pf.get(0).setNum(pf.get(0).getNum()+purchaseOrder.getFreezingQuantity());
//											freezingAssetsService.save(pf.get(0));
//										}else {
//											FreezingAssets freezingAssets1 = new FreezingAssets();
//											freezingAssets1.setMember(member);
//											freezingAssets1.setSubGoods(subGoods);
//											freezingAssets1.setGoodsName(subGoods.getGoodsName());
//											freezingAssets1.setNum(purchaseOrder.getFreezingQuantity());
//											freezingAssets1.setGoodsPrice(order.getPrice());
//											freezingAssets1.setTotalPrice(order.getPrice()*purchaseOrder.getFreezingQuantity());
//											freezingAssetsService.save(freezingAssets1);
//										}
//										purchaseOrder.setFreezingQuantity(0);
									}
//									else {
//										//只加锁仓
//										//给买入的用户加锁仓
//										FreezingAssets freezingAssets = new FreezingAssets();
//										freezingAssets.setMember(member);
//										freezingAssets.setSubGoods(subGoods);
//										List<FreezingAssets> pf = freezingAssetsService.findList(freezingAssets);
//										if (pf.size()>0){
//											pf.get(0).setNum(pf.get(0).getNum()+dealNum);
//											freezingAssetsService.save(pf.get(0));
//										}else {
//											FreezingAssets freezingAssets1 = new FreezingAssets();
//											freezingAssets1.setMember(member);
//											freezingAssets1.setSubGoods(subGoods);
//											freezingAssets1.setGoodsName(subGoods.getGoodsName());
//											freezingAssets1.setNum(purchaseOrder.getFreezingQuantity()-dealNum);
//											freezingAssets1.setGoodsPrice(order.getPrice());
//											freezingAssets1.setTotalPrice(order.getPrice()*(purchaseOrder.getFreezingQuantity()-dealNum));
//										}
//										purchaseOrder.setFreezingQuantity(purchaseOrder.getFreezingQuantity()-dealNum);
//									}
									purchaseOrder.setIsDeal("1");
									purchaseOrder.preUpdate();
									mapper.update(purchaseOrder);
									//给买入账户扣钱
									account.setYue(account.getYue()-(order.getPrice()*num));
									account.preUpdate();
									accountMapper.update(account);
									//成交记录
									TradeRecord tradeRecord = new TradeRecord();
									tradeRecord.setId(IdGen.uuid());
									tradeRecord.setSubGoods(subGoods);
									tradeRecord.setNum(dealNum);
									tradeRecord.setPrice(order.getPrice());
									tradeRecord.setPurchaseMember(member);
									tradeRecord.setPurchaseOrder(orderNo);
									tradeRecord.setSaleMember(order.getMember());
									tradeRecord.setSaleOrderNo(order.getOrderNo());
									tradeRecord.setTotalPrice(dealNum*order.getPrice());
									tradeRecord.preInsert();
									tradeRecordMapper.insert(tradeRecord);
									//给上级加业绩
//									addManagersTeam(member,dealNum*order.getPrice());
									//更新成交价
									manageTradePrice(subGoods,order);

									teamServiceService.manageTeamService(member,c*dealNum,orderNo);

									if ("18888888888".equals(member.getLoginName())){
										subGoodsService.addStock(subGoods,dealNum);
									}
									if ("18888888888".equals(member1.getLoginName())){
										subGoodsService.substractStock(subGoods,dealNum);
									}
									break;
								}

								//处理用户等级
//								memberService.manageMemberType(member);
							}
						}
						aj.setSuccess(true);
						aj.setMsg("买入成功");
					}else {
						aj.setSuccess(true);
						aj.setMsg("操作成功");
					}
				}else {
					aj.setSuccess(false);
					aj.setMsg("余额不足");
				}

			}else {
				aj.setSuccess(false);
				aj.setMsg("账户信息有误");
			}
		}else {
			aj.setSuccess(false);
			aj.setMsg("商品信息有误");
		}

		return aj;
	}

	@Transactional(readOnly = false)
	public void addManagersTeam(Member member, double money) {
		if (member.getP() != null){
			Member pMember = memberService.get(member.getP().getId());
			if (pMember != null){
				while (pMember != null){

					Account account = accountService.get(pMember.getAccount().getId());
					account.setTeam(account.getTeam()+money);
					accountService.save(account);
					if (pMember.getP() != null){
						pMember = memberService.get(pMember.getP().getId());
					}else {
						break;
					}
				}
			}
		}
	}


	public List<PurchaseOrder> findStandardPurchaseOrders(PurchaseOrder purchaseOrder) {
		return mapper.findStandardPurchaseList(purchaseOrder);
	}

	/**
	 * 撤销买入订单
	 * @param member
	 * @param orderNo
	 * @return
	 */
	@Transactional(readOnly = false)
	public synchronized AjaxJson revokePurchaseOrder(Member member, String orderNo) {
		AjaxJson aj = new AjaxJson();
		PurchaseOrder purchaseOrder = new PurchaseOrder();
		purchaseOrder.setMember(member);
		purchaseOrder.setOrderNo(orderNo);
		List<PurchaseOrder> list = findList(purchaseOrder);
		if (list.size()>0){
			for (PurchaseOrder order : list) {
				order.setIsRevoke("1");
				save(order);
			}
		}
		aj.setSuccess(true);
		aj.setMsg("撤销成功");
		return aj;
	}


	@Transactional(readOnly = false)
	public void manageTradePrice(SubGoods subGoods,SaleOrder order){
		//所有此商品的持仓 改动现价
		PositionAssets positionAssets = new PositionAssets();
		positionAssets.setSubGoods(subGoods);
		positionAssets.setIsApply("0");
		List<PositionAssets> assetsList = positionAssetsService.findList(positionAssets);
		if (assetsList.size()>0){
			for (PositionAssets assets : assetsList) {
				if (assets.getGoodsPrice() != 0){
					assets.setNowPrice(order.getPrice());
					assets.setNowTotal(assets.getNowPrice()*assets.getGoodsNum());
					assets.setUpPrice(order.getPrice()-assets.getGoodsPrice());
					assets.setUpPercentage(BigDecimal.valueOf(assets.getUpPrice())
							.divide(BigDecimal.valueOf(assets.getGoodsPrice()),2,BigDecimal.ROUND_HALF_DOWN)
							.multiply(BigDecimal.valueOf(100)).doubleValue());
					assets.preUpdate();
					positionAssetsMapper.update(assets);
				}
			}
		}
		//所有此商品的锁仓 改动现价
		FreezingAssets freezingAssets = new FreezingAssets();
		freezingAssets.setSubGoods(subGoods);
		freezingAssets.setIsApply("0");
		List<FreezingAssets> assetsList1 = freezingAssetsService.findList(freezingAssets);
		if (assetsList1.size()>0){
			for (FreezingAssets assets : assetsList1) {
				assets.setNowPrice(order.getPrice());
				assets.setNowTotal(assets.getNowPrice()*assets.getNum());
				assets.preUpdate();
				freezingAssetsMapper.update(assets);
			}
		}
		//处理手续费收益
//								teamServiceService.manageTeamService(member,charge,orderNo);
		//如果成交价到了原价3倍以上
//		if (BigDecimal.valueOf(order.getPrice()).divide(BigDecimal.valueOf(subGoods.getPrice()),0,BigDecimal.ROUND_DOWN).compareTo(BigDecimal.valueOf(3)) == 0
//				||BigDecimal.valueOf(order.getPrice()).divide(BigDecimal.valueOf(subGoods.getPrice()),0,BigDecimal.ROUND_DOWN).compareTo(BigDecimal.valueOf(3)) == 1){
//			subGoodsService.handleInvestment(subGoods);
//		}
	}

	public List<PurchaseOrder> findLimitList(PurchaseOrder purchaseOrder) {
		return mapper.findLimitList(purchaseOrder);
	}

	@Transactional(readOnly = false)
	public void revokeOrders() {
		mapper.revokeOrders();
	}

	public List<PurchaseOrder> findLimitByPriceList(PurchaseOrder purchaseOrder) {
		return mapper.findLimitByPriceList(purchaseOrder);
	}
}